package org.zjx.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.zjx.dao.entity.DeviceDailyTask;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 
 * @since 2025-05-28
 */
public interface DeviceDailyTaskMapper extends BaseMapper<DeviceDailyTask> {


    // 注解方式（推荐）
    @Select("SELECT ddt.* " +
            "FROM device_daily_task ddt " +
            "INNER JOIN daily_tasks dt ON ddt.daily_tasks_id = dt.id " +
            "${ew.customSqlSegment}")
    List<DeviceDailyTask> selectByConditions(@Param("ew") QueryWrapper<DeviceDailyTask> wrapper);


    /**
     * 自定义UPSERT操作（根据device_id + temporary）
     * @param dailyTasksId 任务ID
     * @param deviceId 设备ID
     * @param temporary 临时标识
     * @return 影响的行数
     */
    @Update({
            "<script>",
            "INSERT INTO device_daily_task (daily_tasks_id, device_id, temporary)",
            "VALUES (#{dailyTasksId}, #{deviceId}, #{temporary})",
            "ON DUPLICATE KEY UPDATE",
            "daily_tasks_id = VALUES(daily_tasks_id)",
            "</script>"
})
int upsertDeviceTask(
        @Param("dailyTasksId") long dailyTasksId,
        @Param("deviceId") long deviceId,
        @Param("temporary") int temporary
);
}
